//
//  ParkingRecordsEngine.m
//  HuLaQuan
//
//  Created by hok on 3/16/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "ParkingRecordsEngine.h"

#import "CurrentUser.h"

@implementation ParkingRecordsEngine

- (void)getParkingRecordsOfCar:(NSString *)carID estate:(NSString *)estateID startTime:(NSString *)sTime endTime:(NSString *)eTime
{
     NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getParkInOutInfo\" sysName=\"\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><communityId>%@</communityId><carPlate>%@</carPlate><startTime>%@</startTime><endTime>%@</endTime></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID, estateID, carID, sTime, eTime];
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_PARKING parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            result = @{@"isSuccess":@YES,@"result":resultDic[@"list"]};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
            
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetParkingRecordsFinish" object:result];
    }];
}

@end
